﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Vehicle_Maintenance
{
    public partial class Car : Form
    {

        private bool dPoint = false;
        private bool updating = false;

        public Car(bool update = false, string vin = "")
        {
            InitializeComponent();
            if (update)
            {
                updating = update;
                DataTable data = this.vehiclesTableAdapter.GetDataByVIN(vin);
                DataRow row = data.Rows[0];

                tb_VIN.Text = row["VIN"].ToString();
                nud_Year.Value = Convert.ToInt32(row["Year"].ToString());
                tb_Make.Text = row["Make"].ToString();
                tb_Model.Text = row["Model"].ToString();
                tb_COdom.Text = row["Orig_Odom"].ToString();
                tb_Name.Text = row["Name"].ToString();
                tb_RegNum.Text = row["Reg_Num"].ToString();
                tb_Varient.Text = row["Model_Vari"].ToString();
                tb_TSize.Text = row["Tire_size"].ToString();
                tb_ESize.Text = row["Eng_Size"].ToString();
                tb_Insur.Text = row["Insur"].ToString();
            }
        }

        #region updates nickname
        private void tbMakeModel_TextChanged(object sender, EventArgs e)
        {
            UpdateNickname();
        }

        private void nudYear_ValueChanged(object sender, EventArgs e)
        {
            UpdateNickname();
        }

        private void UpdateNickname()
        {
            if (!updating)
            {
                tb_Name.Text = tb_Make.Text + " " + tb_Model.Text + " (" + nud_Year.Value + ")";
            }
        } 
        #endregion

        private void b_OK_Click(object sender, EventArgs e)
        {
            if (tb_VIN.Text == "")
            {
                MessageBox.Show("VIN is required", "Required Field", MessageBoxButtons.OK);
            }
            else if (tb_Make.Text == "")
            {
                MessageBox.Show("Make is required", "Required Field", MessageBoxButtons.OK);
            }
            else if (tb_Model.Text == "")
            {
                MessageBox.Show("Model is required", "Required Field", MessageBoxButtons.OK);
            }
            else if (tb_COdom.Text == "")
            {
                MessageBox.Show("Current Odometer is required", "Required Field", MessageBoxButtons.OK);
            }
            else
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void tb_COdom_KeyPress(object sender, KeyPressEventArgs e)
        {
            char a = e.KeyChar;

            if (0x08 == a && tb_COdom.Text.Length != 0)
            {
                if (tb_COdom.Text[tb_COdom.Text.Length - 1] == '.')
                {
                    dPoint = false;
                }
            }

            if (Char.IsLetter(a) | Char.IsPunctuation(a) |
                Char.IsSymbol(a) | ((tb_COdom.TextLength >= 8 && dPoint) | (tb_COdom.TextLength >= 6 && !dPoint)))
            {
                if (a == '.' && !dPoint)
                {
                    dPoint = true;
                }
                else if (0x08 != a)
                {
                    e.Handled = true;
                }
            } // end if    
        }

    }
}
